package com.baizan.daka.base

import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.CallSuper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baizan.base.BaseFragment
import com.baizan.daka.net.BasePageResponse
import com.baizan.daka.widget.CustomLoadMoreView
import com.baizan.daka.widget.ListEmptyErrorView
import com.baizan.daka.widget.MyRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.dianzan.daka.R
import com.scwang.smartrefresh.layout.listener.OnRefreshListener

abstract class BasePageListFragment<P : BasePresenter, A : BaseQuickAdapter<*, *>> :
    BaseFragment<P>(), BaseQuickAdapter.OnItemClickListener {


    var adapter: A? = null
    var myRefreshLayout: MyRefreshLayout? = null
    var mCurrentPageNo = 1

    var mCustomLoadMoreView: CustomLoadMoreView? = null

    /**
     * 记录当前是否在更新
     */
    protected var isRefresh = false

    /**
     * 判断是否正在加载更多
     */
    private var isLoadMore = false

    protected var emptyErrorView: ListEmptyErrorView? = null
    protected var mCurrentResponse: BasePageResponse<*>? = null

    @CallSuper
    override fun initData() {
        setUpRecycleView()
    }

    fun setUpRecycleView() {
        //adapter的包装类
        var mLinearLayoutManager = LinearLayoutManager(context)
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL)
        mRootView?.let {
            val recyclerView = it.findViewById<RecyclerView>(R.id.recycler_view)
            myRefreshLayout = it.findViewById(R.id.refresh_layout)
            recyclerView?.setLayoutManager(mLinearLayoutManager)
            adapter = initAdapter()
            mCustomLoadMoreView = CustomLoadMoreView()
            adapter?.setLoadMoreView(mCustomLoadMoreView)
            recyclerView?.setAdapter(adapter)
            adapter?.setPreLoadNumber(5)
            setPtrListener(recyclerView)
            adapter?.setOnItemClickListener(this)
        }

    }

    protected open fun setPtrListener(recyclerView: RecyclerView) {
        // 下拉刷新
        myRefreshLayout?.setOnRefreshListener(OnRefreshListener { toRefresh() })
        // 加载更多
        adapter?.setOnLoadMoreListener({ toLoadMore() }, recyclerView)
    }

    open fun toRefresh() {
        mCurrentPageNo = 1
        if (isLoadMore) {
            adapter?.loadMoreComplete()
            isLoadMore = false
        }
        onRefresh()
        isRefresh = true
    }

    open fun toLoadMore() {
        mCurrentPageNo = mCurrentPageNo + 1
        if (isRefresh) {
            myRefreshLayout?.refreshComplete()
            isRefresh = false
        }
        onLoadMore()
        isLoadMore = true
    }

    open fun getDatasFail() {
        isRefresh = false
        isLoadMore = false
        this.mCurrentResponse = null
        myRefreshLayout?.refreshComplete()
        adapter?.loadMoreFail()
        //加载失败回到上一次页面数量
        mCurrentPageNo = mCurrentPageNo - 1
        if (mCurrentPageNo < 1) {
            mCurrentPageNo = 1
        }
        if (mCurrentPageNo == 1) {
            setListErrorView()
        }
    }

    open fun <T> getDatasSuccess(response: BasePageResponse<T>?) {
        isRefresh = false
        isLoadMore = false
        mCurrentResponse = response
        myRefreshLayout?.refreshComplete()
        if (response == null) {
            setListEmptyView()
            return
        }
        val list: List<T>? = response.records
        mCurrentPageNo = response.current
        if (mCurrentPageNo <= 1) {
            adapter?.setNewData(list as List<Nothing>)
        } else {
            adapter?.addData(list as List<Nothing>)
        }
        if (mCurrentPageNo >= response.pages) {
            adapter?.loadMoreEnd()
        } else {
            adapter?.loadMoreComplete()
        }
        setListEmptyView()
    }


    //初始化 列表数据的空状态
    protected open fun setListEmptyView() {
        addListEmptyErrorView()
        emptyErrorView?.setEmptyStatusing()
    }

    //初始化 列表数据的错误状态
    protected open fun setListErrorView() {
        addListEmptyErrorView()
        emptyErrorView?.setErrorStatusing()
    }

    open fun addListEmptyErrorView() {
        if (adapter?.emptyViewCount == 0) {
            if (emptyErrorView == null) {
                context?.let {
                    emptyErrorView = ListEmptyErrorView(it)
                }
                initListEmptyErrorData(emptyErrorView)
            }
//            emptyErrorView.setErrorClickListener(View.OnClickListener {
//                if (myRefreshLayout != null) {
//                    myRefreshLayout!!.autoRefresh()
//                } else {
//                    onRefresh()
//                }
//            })

            var vg = emptyErrorView?.parent
            vg?.let {
                var viewGroup = it as ViewGroup
                viewGroup.removeView(emptyErrorView)
            }
            adapter?.emptyView = emptyErrorView
        }
    }

    //默认的，特殊需要的重写
    protected open fun initListEmptyErrorData(view: ListEmptyErrorView?) {}


    override fun onItemClick(
        adapter: BaseQuickAdapter<*, *>?,
        view: View?,
        position: Int
    ) {

    }

    abstract fun onRefresh()

    abstract fun onLoadMore()

    /**
     * 创建数据的Adapter
     * 要求实现自己的adapter
     */
    abstract fun initAdapter(): A?
    override fun layoutId(): Int = R.layout.frg_base_list
}